package com.neusoft.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.neusoft.model.BooksModel;
import com.neusoft.model.BookshelfModel;
import com.neusoft.model.entity.Books;
import com.neusoft.model.entity.Bookshelf;
import com.neusoft.model.vo.BooksModelVO;
import com.neusoft.service.BooksService;
import com.neusoft.service.BookshelfService;

@Controller
@RequestMapping("/bookshelf")
public class BookshelfController {

	@Autowired
	BooksService booksService;
	
	@Autowired
	BookshelfService bookshelfService;


	@RequestMapping("/query")
	public String query(String id,Model model){
//		BooksModel bbb=new BooksModel();
//		bbb.setId(123);
//		booksModelVO.setBooksModel(bbb);
//		booksModelVO.setId("123");
		List<Bookshelf> bookshelf_list = new ArrayList<Bookshelf>();
		List<BookshelfModel> bookshelf_listTmps = new ArrayList<BookshelfModel>();
		bookshelf_list=bookshelfService.findBooksByActiveId(id);
		System.out.println(bookshelf_list.size()+"b");
		for (int i=0;i<bookshelf_list.size();i++){
			int book_id=bookshelf_list.get(i).getBookId();
			System.out.println(book_id+"f");
			BooksModelVO booksModelVO=new BooksModelVO();
			BooksModel booksModel=new BooksModel();
			booksModel.setId(book_id);
			booksModelVO.setBooksModel(booksModel);
			Books books=booksService.findBooksbyID(book_id);
//			System.out.print(books.size());
			BookshelfModel bookshelf_listTmp=new BookshelfModel();
			bookshelf_listTmp.setAuthor(books.getAuthor());
			bookshelf_listTmp.setBookName(books.getBookName());
			bookshelf_listTmp.setPublish(books.getPublish());
			bookshelf_listTmp.setCallno(books.getCallno());
			bookshelf_listTmp.setBookId(book_id);
			bookshelf_listTmps.add(bookshelf_listTmp);
		}
//		System.out.println(bookshelf_list.size()+"dfd");
//		System.out.println(bookshelf_list.get(1)+"dfd");
		model.addAttribute("bookshelf",bookshelf_listTmps);
		
		return "bookshelf";
		
	}	
	
	@RequestMapping("/insert")
	public String insert(String user_id,String book_id,Model model){
		Bookshelf bookshelf=new Bookshelf();
		bookshelf.setBookId(Integer.valueOf(book_id));
		bookshelf.setId(user_id);
		bookshelfService.insertBookshelf(bookshelf);
		model.addAttribute("user_id",user_id);
		return "bookShelfTmp";
	}
	
	@RequestMapping("/delete")
	public String delete(String user_id,String book_id,Model model){
		
		bookshelfService.deleteBookshelf(Integer.valueOf(book_id), user_id);
		model.addAttribute("user_id",user_id);
		return "bookShelfTmp";
	}
	
}
